#include <future>
#include <iostream>
#include <thread>

//两个函数 async()和package_task<>() 前一个可以在子线程执行，后者在当前线程执行，
//相同点，都能获取std::future
//
int task(int a, int b) {
    int ret_a = a * a;
    int ret_b = b * 2;
    std::cout << "this thread id : " << std::this_thread::get_id() << std::endl;
    return ret_a + ret_b;
}

int main()
{
    std::cout << "main thread id : " << std::this_thread::get_id() << std::endl;
    std::future<int> f = std::async(task, 1, 2);
    //std::future<int> f = std::async(task, 1, 2); 
    //第一个参数 可以是std::launch::async(默认参数，开启线程完成任务) 或 std::launch::deferred（不开线程，当作函数延期使用）
    std::cout << f.get() << std::endl;
    std::cout << "main thread id : " << std::this_thread::get_id() << std::endl;

    std::packaged_task<int(int, int)> t(task);
    t(1,2);
    std::cout << t.get_future().get() << std::endl;
    return 0;
}